﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Castle.Core.Logging;

namespace TVN.ShopMan.ApplicationInterface.AppForms
{
    using TVN.Common.Interfaces;
    using TVN.ShopMan.ActiveModels;
    using TVN.ShopMan.ApplicationInterface.Helpers;

    public partial class AccountFormEdit : Form
    {
        /// <summary>
        /// All the modes of editing an account.
        /// </summary>
        public enum EditModes
        {
            Default,
            PasswordFocus
        }

        private static readonly ILogger logger = Program.Logger.CreateChildLogger("AccountFormEdit");
        private IRepository<Account> accountRepository = null;
        private Account selectedAccount = null;
        private EditModes editMode = EditModes.Default;

        #region Properties

        public Account SelectedAccount
        {
            get { return this.selectedAccount; }
            set { this.selectedAccount = value; }
        }

        #endregion

        #region Constructors

        public AccountFormEdit()
        {
            InitializeComponent();

            this.SetUp();
        }

        public AccountFormEdit(int accountId, EditModes mode)
        {
            InitializeComponent();

            this.SetUp();

            this.editMode = mode;
            if (accountId > 0)
            {
                this.selectedAccount = this.accountRepository.GetById(accountId);
            }

            this.LoadAccountData();
        }

        public AccountFormEdit(Account accountItem, EditModes mode)
        {
            InitializeComponent();

            this.SetUp();

            this.editMode = mode;
            this.selectedAccount = accountItem;

            this.LoadAccountData();
        }

        #endregion

        #region Private methods

        private void SetUp()
        {
            this.accountRepository = Program.Container.Resolve<IRepository<Account>>();
        }

        private void LoadAccountData()
        {
            if (this.selectedAccount != null)
            {
                this.txtUsername.Text = this.selectedAccount.Username;
                this.txtPassword.Text = this.selectedAccount.Password;
                this.txtPassword2.Text = this.selectedAccount.Password;
                this.ckbActive.Checked = this.selectedAccount.IsActive;
                this.txtEmail.Text = this.selectedAccount.Email;
                this.txtFullname.Text = this.selectedAccount.FullName;
            }

            switch (this.editMode)
            {
                case EditModes.PasswordFocus:
                    this.txtPassword.Focus();
                    break;
            }
        }

        /// <summary>
        /// Assign current information into Account object
        /// </summary>
        /// <returns></returns>
        private void AssignAccountData()
        {
            try
            {
                if (this.selectedAccount == null)
                {
                    this.selectedAccount = new Account
                    {
                        CreatedDate = DateTime.Now,
                        Creator = Program.SessionData.ActiveAccount
                    };
                }
                else
                {
                    this.selectedAccount.LastModifiedDate = DateTime.Now;
                    this.selectedAccount.LastModifier = Program.SessionData.ActiveAccount;
                }

                this.selectedAccount.Username = this.txtUsername.Text;
                this.selectedAccount.Password = this.txtPassword.Text;
                this.selectedAccount.Email = this.txtEmail.Text;
                this.selectedAccount.FullName = this.txtFullname.Text;
                this.selectedAccount.IsActive = this.ckbActive.Checked;
            }
            catch (Exception ex)
            {
                logger.Warn("Could not save account", ex);
            }
        }

        #endregion

        #region Events

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isCreating = ((this.selectedAccount == null) || (this.selectedAccount.Id <= 0));
            if (!this.txtPassword.Text.Equals(this.txtPassword2.Text))
            {
                this.txtPassword.Focus();
            }
            else if (AppHelper<Account>.IsExists(Account.Properties.Username, this.txtUsername.Text, isCreating))
            {
                this.txtUsername.Focus();
            }
            else if (AppHelper<Account>.IsExists(Account.Properties.Email, this.txtEmail.Text, isCreating))
            {
                this.txtEmail.Focus();
            }
            else
            {
                this.AssignAccountData();
                this.DialogResult = DialogResult.OK;
            }
        }

        #endregion
    }
}
